package com.example.badyloans.controller;

import com.example.badyloans.common.ReturnResult;
import com.example.badyloans.pojo.*;
import com.example.badyloans.servlet.TSystemDictionaryItemService;
import com.example.badyloans.servlet.TSystemDictionaryService;
import com.example.badyloans.util.PageSupport;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 啊桥哦
 * @Description:
 * @date 2020-07-17 10:24
 */
@Api(tags = "字典表查询等操作")
@RestController
public class systemdictionaryQuery {
    @Resource
    private TSystemDictionaryService tSystemDictionaryService;
    @Resource
    private  TSystemDictionaryItemService tSystemDictionaryItemService;

    /**
     * 查询数据字典
     * @param systemd
     * @return
     */
    @PostMapping("/system/dictionary/query")
    public ReturnResult dictionaryquery(systemdictionary systemd){
        PageSupport page=new PageSupport();
        page.setPageSize(5);
        page.setTotalCount(tSystemDictionaryService.fenyesum(systemd.getKeyword())); //查询总记录数
        page.setCurrentPageNo(systemd.getCurrentPage());
        Integer pageSize=page.getPageSize();       //拿到页大小
        Integer currentPageNo=page.getCurrentPageNo(); //拿到页码
        List<TSystemDictionary> list=tSystemDictionaryService.selectquan(systemd.getKeyword(),((currentPageNo-1)*pageSize),pageSize);
        if (list==null){
            return  ReturnResult.fail("没有数据");
        }
        return ReturnResult.success(list,page);
    }

    /**
     * 修改数据字典数据
     * @param tSystemDictionary
     * @return
     */
    @PostMapping("/system/dictionary/update")
    public ReturnResult dictionaryupdate(TSystemDictionary tSystemDictionary){
        int count=tSystemDictionaryService.updateByPrimaryKeySelective(tSystemDictionary);
        if(count>0){
            return ReturnResult.succes();
        }
        return ReturnResult.fail("修改失败");
    }

    /**
     * 新增数据字典数据
     * @param tSystemDictionary
     * @return
     */
    @PostMapping("system/dictionary/add")
    public ReturnResult dictionaryadd(TSystemDictionary tSystemDictionary){
        if(tSystemDictionary==null){
            return ReturnResult.fail("该数据为空");
        }
        int count=tSystemDictionaryService.insertSelective(tSystemDictionary);
        if(count>0){
            return ReturnResult.succes();
        }
        return ReturnResult.fail("新增失败");
     }

    /**
     * 查询字典表的子表
     * @return
     */
    @PostMapping("system/dictionary/getAll")
    public ReturnResult dictionarygetAll(){
        List<TSystemDictionary> list=tSystemDictionaryService.selectchaxunquanbu();
        return ReturnResult.success(list,null);
    }

    /**
     * 查询系统数据字典取值表数据
     * @param systemd
     * @param parentId
     * @return
     */
    @PostMapping("system/dictionaryItem/query")
    public ReturnResult dictionaryItem(systemdictionary systemd , @RequestParam(required = false) Integer parentId){
        PageSupport page=new PageSupport();
        page.setPageSize(5);
        page.setTotalCount(tSystemDictionaryItemService.selectchaxunquanbu2(parentId,systemd.getKeyword())); //查询总记录数
        page.setCurrentPageNo(systemd.getCurrentPage());
        Integer pageSize=page.getPageSize();       //拿到页大小
        Integer currentPageNo=page.getCurrentPageNo(); //拿到页码
        List<TSystemDictionaryItem> list=tSystemDictionaryItemService.selectchaxunquanbu1(systemd.getKeyword(),parentId,((currentPageNo-1)*pageSize),pageSize);
        if (list==null){
            return  ReturnResult.fail("没有数据");
        }
        return ReturnResult.success(list,page);
    }

    /**
     * 修改字典表数据
     * @param tSystemDictionaryItem
     * @return
     */
    @PostMapping("system/dictionaryItem/update")
    public ReturnResult dictionaryitemupdate(TSystemDictionaryItem tSystemDictionaryItem){
        int count=tSystemDictionaryItemService.updateByPrimaryKeySelective(tSystemDictionaryItem);
        if(count>0){
            return ReturnResult.succes();
        }
        return ReturnResult.fail("修改失败");
    }

    /**
     * 新增字典表数据
     * @param tSystemDictionaryItem
     * @return
     */
    @PostMapping("system/dictionaryItem/add")
    public ReturnResult dictionaryItemadd(TSystemDictionaryItem tSystemDictionaryItem){
        int count=tSystemDictionaryItemService.insertSelective(tSystemDictionaryItem);
        if (count>0) {
            return ReturnResult.succes();
        }
        return ReturnResult.fail("修改失败");
    }




}
